package com.fishery.base.util;

import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Excel文件处理工具类
 */
public class ExcelUtil {

	public static void write(HttpServletResponse response, String fileName, Class<?> obj, String sheet, List<?> records) {
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
			response.setContentType("application/vnd.ms-excel");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
			EasyExcel.write(response.getOutputStream(), obj).sheet(sheet).doWrite(records);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
